package com.jtjrenren.android.taxi.driver.manger.push;

import com.jtjrenren.android.taxi.driver.bean.control.CenterResponse;
import com.jtjrenren.android.taxi.driver.bean.control.CmdContentBase;
import com.jtjrenren.android.taxi.driver.bean.event.ControlCenterEvent;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.manger.push.cmd.ControlCmd;
import com.jtjrenren.android.taxi.driver.manger.push.cmd.Protocl;
import com.jtjrenren.android.taxi.driver.manger.push.cmd.ReceiveControlCmd;

import com.library.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by Administrator on 2016/5/12.
 */
public class PushCmdEngine {
    public static void handReceiveMsg(String receiveMsg) {
        LogUtil.log("push", "处理消息的内容为：" + receiveMsg);
        CenterResponse centerResponse = ReceiveControlCmd.parseCenterResponse(receiveMsg);
        LogUtil.log("handmsg",centerResponse.toString());
        if (centerResponse == null)
            return;
        String msgID = centerResponse.getCenterMsgID();
        String content = centerResponse.getContent();
        CmdContentBase response = null;
        if (Protocl.CMD_RESPONSE_COMMON.equals(msgID)) {//中心通用回复
            response = ReceiveControlCmd.parseCommonResponse(content);

        } else if (Protocl.CMD_RESPONSE_XFDD.equals(msgID)) {//下发订单
            response = ReceiveControlCmd.parseXDResponse(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        } else if (Protocl.CMD_RESPONSE_QDJG.equals(msgID)) {//抢单结果
            response = ReceiveControlCmd.parseQdResultResponse(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        } else if (Protocl.CMD_RESPONSE_QJCK.equals(msgID)) {//去接乘客
            response = ReceiveControlCmd.parseQrQjCkResponse(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        } else if (Protocl.CMD_RESPONSE_QRSC.equals(msgID)) {//确认乘客上车
            response = ReceiveControlCmd.parseQrCkScResponse(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        } else if (Protocl.CMD_RESPONSE_QXDD.equals(msgID)) {//取消订单
            response = ReceiveControlCmd.parseZxQxDdResponse(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        } else if (Protocl.CMD_RESPONSE_TBQXDD.equals(msgID)) {//同步取消订单
        } else if (Protocl.CMD_RESPONSE_XFCF.equals(msgID)) {//下发车费
            response = ReceiveControlCmd.parseCostResponse(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        } else if (Protocl.CMD_RESPONSE_CKZFXX.equals(msgID)) {//乘客支付信息
            response = ReceiveControlCmd.parsePayResultResponse(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        } else if (Protocl.CMD_RESPONSE_QZXB.equals(msgID)) {//强制下班
            response = ReceiveControlCmd.parseQzSjXb(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        }else if (Protocl.CMD_RESPONSE_OTHERLOGIN.equals(msgID)){//其他设备登陆
            LogUtil.log("push", "其他设备登陆的消息" );
            LogUtil.log("control","其他设备登陆的消息！");
            response = ReceiveControlCmd.parseOtherDutyOn(content);

            sendCommonToCenter(centerResponse.getCenterSerno(), centerResponse.getCenterMsgID(), 0);
        }

        if (response != null) {
            LogUtil.log("push", "将消息通过bus发送出去: " + response.toString());
            EventBus.getDefault().post(new ControlCenterEvent(response.getBrocastAction(), response));
        }
    }

    /**
     * 回复通用应答
     */
    private static void sendCommonToCenter(int centerMsgSer, String centerMsgID, int msgResult) {
        String commonResponse = ControlCmd.getCommonResponse(centerMsgSer, centerMsgID, msgResult);
        AppManger.getInstance().sendMsg(commonResponse);
    }
}
